package com.crm.service.impl.productManager;

import com.crm.common.model.Enum.DictionaryEnum;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.productManagerCustomMapper.CustomerProductCustomMapper;
import com.crm.dao.mapper.productManager.CustomerProductMapper;
import com.crm.model.entity.productManager.CustomerProduct;
import com.crm.model.entity.productManager.CustomerProductExample;
import com.crm.model.qo.productManager.CustomerProductQO;
import com.crm.model.qo.productManager.CustomerProductUpdateQO;
import com.crm.model.vo.orderManager.FinanceIndexOrderProductVO;
import com.crm.model.vo.productManager.CustomerProductVO;
import com.crm.model.vo.productManager.ProductVO;
import com.crm.service.productManager.CustomerProductDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yincuimei
 * @Date 2021/1/27 15:22
 */
@Service
public class CustomerProductDbServiceImpl implements CustomerProductDbService {
    @Autowired
    private CustomerProductCustomMapper customerProductCustomMapper;
    @Autowired
    private CustomerProductMapper customerProductMapper;

    /**
     * 产品管理--产品列表
     * @param customerProductQO  ---产品列表查询参数
     * @param orderStr           ---产品列表排序字段及规则
     * @return
     */
    @Override
    public List<CustomerProductVO> selectCustomerProductVOList(CustomerProductQO customerProductQO, String orderStr) {
        return customerProductCustomMapper.selectCustomerProductVOList(customerProductQO,orderStr);
    }

    /**
     * 优惠券页面，为了和会员产品在同一列表展示，统一使用会员产品字段名
     * @param customerProductQO  ---产品列表查询参数
     * @param orderStr           ---产品列表排序字段及规则
     * @return
     */
    @Override
    public List<Map<String,Object>> selectCustomerProductVOListForCoupon(CustomerProductQO customerProductQO, String orderStr) {
        return customerProductCustomMapper.selectCustomerProductVOListForCoupon(customerProductQO,orderStr);
    }

    /**
     * 产品管理--根据产品ID和删除状态查询产品信息(产品编辑)
     * @param customerProductId  ---产品ID
     * @param deleteStatus       ---删除状态
     * @return
     */
    @Override
    public CustomerProduct selectCustomerProductById(String customerProductId, short deleteStatus) {
        CustomerProductExample customerProductExample = new CustomerProductExample();
        CustomerProductExample.Criteria criteria = customerProductExample.createCriteria();
        criteria.andCustomerProductIdEqualTo(customerProductId);//产品ID
        criteria.andDeleteStatusEqualTo(deleteStatus);//删除状态
        List<CustomerProduct> customerProductList = customerProductMapper.selectByExampleWithBLOBs(customerProductExample);
        return customerProductList.size() > 0 ? customerProductList.get(0) : null;
    }

    /**
     * 产品管理--产品列表(上/下架操作)
     * @param customerProduct
     * @return
     */
    @Override
    public Boolean updateCustomerProductShelfStatus(CustomerProduct customerProduct) {
        return customerProductCustomMapper.updateCustomerProductById(customerProduct) > 0;
    }

    /**
     * 产品管理--编辑产品
     * @param customerProductUpdateQO ---产品编辑信息
     * @return
     */
    @Override
    public Boolean updateCustomerProductById(CustomerProductUpdateQO customerProductUpdateQO) {
        CustomerProduct customerProduct = new CustomerProduct();
        BeanUtils.copyProperties(customerProductUpdateQO,customerProduct);
        return customerProductMapper.updateByPrimaryKeySelective(customerProduct) > 0;
    }

    /**
     * 产品管理--查询产品名称是否已存在
     * @param productName   ---产品名称
     * @param deleteStatus  ---删除状态
     * @return
     */
    @Override
    public Boolean isExistProductName(String productName, short deleteStatus) {
        CustomerProductExample customerProductExample = new CustomerProductExample();
        CustomerProductExample.Criteria criteria = customerProductExample.createCriteria();
        criteria.andProductNameEqualTo(productName);//产品名称
        criteria.andDeleteStatusEqualTo(deleteStatus);//删除状态
        List<CustomerProduct> customerProductList = customerProductMapper.selectByExample(customerProductExample);
        return customerProductList != null && customerProductList.size() > 0;
    }

    /**
     * 产品管理--查询产品编码是否已存在
     * @param productCode   ---产品编码
     * @param deleteStatus  ---删除状态
     * @return
     */
    @Override
    public Boolean isExistProductCode(String productCode, short deleteStatus) {
        CustomerProductExample customerProductExample = new CustomerProductExample();
        CustomerProductExample.Criteria criteria = customerProductExample.createCriteria();
        criteria.andProductCodeEqualTo(productCode);//产品编码
        criteria.andDeleteStatusEqualTo(deleteStatus);//删除状态
        List<CustomerProduct> customerProductList = customerProductMapper.selectByExample(customerProductExample);
        return customerProductList != null && customerProductList.size() > 0;
    }

    /**
     * 产品管理--查询专题ID是否已存在
     * @param topicId       ---专题ID
     * @param deleteStatus  ---删除状态
     * @return
     */
    @Override
    public Boolean isExistTopicId(Integer topicId, short deleteStatus) {
        CustomerProductExample customerProductExample = new CustomerProductExample();
        CustomerProductExample.Criteria criteria = customerProductExample.createCriteria();
        criteria.andTopicIdEqualTo(topicId);//产品编码
        criteria.andDeleteStatusEqualTo(deleteStatus);//删除状态
        List<CustomerProduct> customerProductList = customerProductMapper.selectByExample(customerProductExample);
        return customerProductList != null && customerProductList.size() > 0;
    }

    /**
     * 产品管理--新增产品
     * @param customerProduct  ---产品信息
     * @return
     */
    @Override
    public Boolean insertCustomerProduct(CustomerProduct customerProduct) {
       return customerProductMapper.insertSelective(customerProduct) > 0;
    }

    @Override
    public List<CustomerProduct> selectProductDropDownList(String productName) {
        CustomerProductExample customerProductExample = new CustomerProductExample();
        CustomerProductExample.Criteria criteria = customerProductExample.createCriteria();
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        if(!StringUtils.isBlank(productName)){
            criteria.andProductNameLike("%" + productName + "%");
        }
        customerProductExample.setOrderByClause("create_tm desc");
        List<CustomerProduct> customerProductList = customerProductMapper.selectByExample(customerProductExample);
        return customerProductList;
    }

    @Override
    public CustomerProduct selectCustomerProductByProductId(String productId) {
        CustomerProduct customerProduct = customerProductMapper.selectByPrimaryKey(productId);
        return customerProduct;
    }

    /**
     * 商机-新增商机-查询所有产品列表
     * @param productName 产品名称
     * @param order 分组
     * @return
     */
    @Override
    public List<CustomerProduct> productList(String productName,String order,String productCategories) {
        CustomerProductExample customerProductExample = new CustomerProductExample();
        customerProductExample.setOrderByClause(order);
        CustomerProductExample.Criteria criteria = customerProductExample.createCriteria();
        criteria.andProductNameLike("%"+productName +"%");
/*        if(!StringUtils.isBlank(productCategories)){
            criteria.andProductCategoriesEqualTo(productCategories);
        }*/
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));//删除状态
        List<CustomerProduct> customerProductList = customerProductMapper.selectByExample(customerProductExample);
        return customerProductList;
    }

    /***
     * @Description:查询所有产品列表（非会员产品表和会员产品表）
     * @Param:产品名称
     * @Author: luojie
     * @Date: 2021/5/7 11:29
     */
    @Override
    public List<HashMap> selectALLProductDropDownList(String productName) {
        List<HashMap> productList = customerProductCustomMapper.selectAllProductDropDownList(productName);
        return productList;
    }

    /***
     * @Description:查询没添加的会员课程产品
     * @Param: productName 产品名称
     * @Param: memberProductRelationId 会员产品关系id
     * @Author: luojie
     * @Date: 2021/5/13 9:22
     */
    @Override
    public List<CustomerProductVO> selectNotAddCustomerMemberProductCourse(String productName, String memberProductRelationId) {
        List<CustomerProductVO> productList = customerProductCustomMapper.selectNotAddCustomerMemberProductCourse(productName,memberProductRelationId, DictionaryEnum.EXCHANGE_METHOD.getTypeCode());
        return productList;
    }

    /**
     * 根据产品名称或者产品编码查询产品信息
     * @Author pym
     * @Date  2021/6/3 16:37
     * @param productName 产品名称
     * @param productCode 产品编码
     * @return {@link {com.crm.model.entity.productManager.CustomerProduct}}
     */
    @Override
    public CustomerProduct selectCustomerProductByNameOrCode(String productName, String productCode) {
        CustomerProductExample customerProductExample = new CustomerProductExample();
        CustomerProductExample.Criteria criteria = customerProductExample.createCriteria();
        criteria.andProductNameEqualTo(productName);//产品名称
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));//删除状态
        if (StringUtils.isNotBlank(productCode)) {
            CustomerProductExample.Criteria criteria2 = customerProductExample.createCriteria();
            criteria2.andProductCodeEqualTo(productCode);//产品编码
            criteria2.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));//删除状态
            customerProductExample.or(criteria2);
        }
        List<CustomerProduct> customerProductList = customerProductMapper.selectByExample(customerProductExample);
        if (customerProductList != null && customerProductList.size() > 0) {
            return customerProductList.get(0);
        }
        return null;
    }

    /**
     * 根据专题id查询产品信息
     * @Author pym
     * @Date  2021/7/29 15:00
     * @param topicId 专题id
     * @return {@link {com.crm.model.entity.productManager.CustomerProduct}}
     */
    @Override
    public CustomerProduct selectCustomerProductByTopicId(String topicId) {
        CustomerProductExample customerProductExample = new CustomerProductExample();
        CustomerProductExample.Criteria criteria = customerProductExample.createCriteria();
        criteria.andTopicIdEqualTo(Integer.parseInt(topicId));//专题id
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));//删除状态
        List<CustomerProduct> customerProductList = customerProductMapper.selectByExample(customerProductExample);
        if (customerProductList != null && customerProductList.size() > 0) {
            return customerProductList.get(0);
        }
        return null;
    }

    /**
     * 订单产品列表
     * @param dateUnit
     * @return
     */
    @Override
    public List<FinanceIndexOrderProductVO> financeIndexOrderProductList(Integer dateUnit,String companyId) {
        return customerProductCustomMapper.financeIndexOrderProductList(dateUnit,companyId);
    }

    /**
     * 根据产品编码查询产品信息
     * @Author pym
     * @Date  2021/8/16 16:36
     * @param productCode 产品编码
     * @return {@link {com.crm.model.entity.productManager.CustomerProduct}}
     */
    @Override
    public CustomerProduct selectCustomerProductByCode(String productCode) {
        CustomerProductExample customerProductExample = new CustomerProductExample();
        CustomerProductExample.Criteria criteria = customerProductExample.createCriteria();
        criteria.andProductCodeEqualTo(productCode);//产品编码
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));//删除状态
        List<CustomerProduct> customerProductList = customerProductMapper.selectByExample(customerProductExample);
        if (customerProductList != null && customerProductList.size() > 0) {
            return customerProductList.get(0);
        }
        return null;
    }

    @Override
    public List<Map<String,String>> selectProductByIdList(List<Map<String,String>> productIdList) {
        return customerProductCustomMapper.selectProductByIdList(productIdList);
    }

    /**
     * 查询非会员上架产品
     * @Author pym
     * @Date  2021/12/29 17:00
     * @param
     * @return {@link {java.util.List<com.crm.model.vo.productManager.ProductVO>}}
     */
    @Override
    public List<ProductVO> selectNonMemberShelfProductList() {
        return customerProductCustomMapper.selectNonMemberShelfProductList();
    }

}
